package com.xiaoxx.controller.coin;


import com.xiaoxx.common.base.JwtUser;
import com.xiaoxx.common.result.R;
import com.xiaoxx.model.entity.coin.BinanceApikey;
import com.xiaoxx.model.vo.coin.UserProfitAndLossInfoVo;
import com.xiaoxx.service.BinanceOderService;
import com.xiaoxx.service.BinanceTradingService;
import com.xiaoxx.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author HX
 * @since 2024-12-09
 */
@RestController
@RequestMapping("/api/binanceOder")
public class BinanceOderController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private BinanceTradingService tradingService;

    @Autowired
    private BinanceOderService binanceOderService;

    @RequestMapping(value = "/getUserProfitAndLossInfo", method = RequestMethod.GET)
    @ApiOperation("账户成交历史")
    public R<UserProfitAndLossInfoVo> getUserProfitAndLossInfo() {
        UserProfitAndLossInfoVo vo = new UserProfitAndLossInfoVo();
        JwtUser jwtUser = JwtUtil.getJwtUser(request);
        BinanceApikey binanceApikey = tradingService.getBinanceApikey(jwtUser.getUserId());
        if (binanceApikey == null) {
            return R.success(new UserProfitAndLossInfoVo());
        }
        BigDecimal dayProfitAndLoss = binanceOderService.getTodayProfitAndLoss(jwtUser.getUserId());
        vo.setTotalUnrealizedProfit(new BigDecimal(binanceApikey.getTotalUnrealizedProfit()).setScale(2, BigDecimal.ROUND_HALF_UP));
        vo.setTotalCrossWalletBalance(new BigDecimal(binanceApikey.getTotalCrossWalletBalance()).setScale(2, BigDecimal.ROUND_HALF_UP));
        vo.setTotalWalletBalance(new BigDecimal(binanceApikey.getTotalWalletBalance()).setScale(2, BigDecimal.ROUND_HALF_UP));
        vo.setDayProfitAndLoss(dayProfitAndLoss.add(vo.getTotalUnrealizedProfit()));
        return R.success(vo);
    }
}

